Param(
  [int]$MaxChars = 9000  # 适配一次性粘贴；需要更短可改 6000~7000
)

$ErrorActionPreference = "Stop"
$root = Split-Path -Parent $MyInvocation.MyCommand.Path
Set-Location (Join-Path $root "..")  # 到仓库根

$targets = @(
  ".specify/memory/constitution.md",
  "specs/auto-shoot-core-loop/spec.md",
  "specs/auto-shoot-core-loop/plan.md",
  "specs/auto-shoot-core-loop/tasks.md",
  "specs/auto-shoot-core-loop/checklists.md",
  "README.md"
) | Where-Object { Test-Path $_ }

$header = @"
# Project Primer · 搜打撤：自动射击

本文件面向“新会话/新助理”的快速对齐，视为唯一权威摘要。
引擎与平台：Cocos Creator 3.8 · WeChat MiniGame（2D正交+Y轴2.5D）
硬约束：主包≤2MB；大厅<40 DC / 战斗<120 DC；IAA频控（每局≤2、日≤5）
核心系统：AutoAim/AutoFire/AutoReload、背包与6配件槽、保险箱（禁武器/护甲）、HUD
——以下为裁剪自宪章/规格/计划/任务/清单的要点——
"@

# 读取并做轻裁剪：仅保留 #/##/### 标题、小节首段、列表前若干行
function Trim-Doc([string]$path) {
  $txt = Get-Content $path -Raw
  # 简易保留策略：取前 3500 字符，避免过长
  $limit = 3500
  if ($txt.Length -gt $limit) { $txt = $txt.Substring(0, $limit) + "`n...(trimmed)" }
  return "`n`n---`n`n## Source: $path`n`n$txt"
}

$body = ($targets | ForEach-Object { Trim-Doc $_ }) -join "`n"
# 追加目录与关键路径
$tree = (Get-ChildItem -Recurse -Directory -Path "game","server","shared" |
  Select-Object -First 60 | ForEach-Object { $_.FullName.Replace((Get-Location).Path,'').TrimStart('\') }) -join "`n- "
$treeBlock = "`n`n---`n`n## Repo Glance (top dirs)`n- $tree"

$out = ($header + $body + $treeBlock)

# 控制总长度
if ($out.Length -gt $MaxChars) {
  $out = $out.Substring(0, $MaxChars) + "`n`n...(truncated for context window)"
}

New-Item -ItemType Directory -Force -Path "docs" | Out-Null
$out | Set-Content -NoNewline -Path "docs/primer.md" -Encoding UTF8

Write-Host "✅ generated docs/primer.md (" ($out.Length) "chars )"
